<template>
  <BasicModal
    v-bind="$attrs"
    @register="registerModal"
    @ok="handleSubmit"
    :title="getTitle"
    :wrapperProps="{
      minHeight: 300,
    }"
  >
    <div class="pt-3px pr-3px">
      <BasicForm @register="registerForm" />
    </div>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref, unref, computed } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { attrFormConfig } from './attrData';
  import { addModelAttrList, modifyModelAttrList } from '/@/api/sys/gateway/gateway';
  export default defineComponent({
    components: { BasicModal, BasicForm },
    props: {
      formData: { type: Object },
    },
    emits: ['submit', 'register'],
    setup(_, { emit }) {
      const isUpdate = ref(true);
      const group_id = ref();
      let protocol_type = ref('');
      const [registerForm, { setFieldsValue, resetFields, validate }] = useForm(attrFormConfig);

      const [registerModal, { setModalProps, closeModal }] = useModalInner((data) => {
        resetFields();
        data && onDataReceive(data);
      });

      function onDataReceive(data) {
        //获取分组id和驱动类型
        if (data.isUpdate == false) {
          group_id.value = data.rowdata.group_id;
          protocol_type.value = data.rowdata.protocol_type;
        } else {
          let protocoltype = data.rowData.protocol_type;
          protocol_type.value = protocoltype;
        }

        setModalProps({ confirmLoading: false });
        isUpdate.value = !!data?.isUpdate;
        if (unref(isUpdate)) {
          setFieldsValue(data.rowData);
        }
      }
      const getTitle = computed(() => (!unref(isUpdate) ? '新增属性' : '编辑属性'));
      async function handleSubmit() {
        try {
          const obj = await validate();
          setModalProps({ confirmLoading: true });
          let values = obj;
          values.protocol_type = protocol_type.value;
          let rule = {
            min_value: obj.min_value,
            max_value: obj.max_value,
            step_length: obj.step_length,
            unit: obj.unit,
            enum_value: obj.enum_value,
            enum_desc: obj.enum_desc,
            bool_false: obj.bool_false,
            bool_true: obj.bool_true,
            data_length: obj.data_length,
            time_fmt: obj.time_fmt,
            address: obj.address,
            function_code: obj.function_code,
            objects_count: obj.objects_count,
            byte_order: obj.byte_order,
            word_order: obj.word_order,
            operator: obj.operator,
            operation_value: obj.operation_value,
            value_order: obj.value_order,
            registers_count: obj.registers_count,
          };

          values.rule = rule;

          if (unref(isUpdate)) {
            await modifyModelAttrList(values);
          } else {
            obj.group_id = group_id.value;
            await addModelAttrList(values);
          }
          closeModal();
          emit('submit', { isUpdate: unref(isUpdate), isGroup: false });
        } finally {
          setModalProps({ confirmLoading: false });
        }
      }
      return { registerModal, registerForm, handleSubmit, getTitle };
    },
  });
</script>
